package com.ruoyi.app.controller.marketing.mapper;

import com.ruoyi.app.controller.marketing.entity.BuySpuUserVO;
import com.ruoyi.common.entity.VO.CouponSearchVO;
import com.ruoyi.common.entity.VO.CouponVO;
import com.ruoyi.common.core.dao.BaseMapper;
import com.ruoyi.common.entity.DO.marketing.CouponDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ICouponMapper extends BaseMapper<CouponDO> {
    List<CouponVO> searchList(CouponSearchVO couponSearchVO);

    @Select("SELECT title, type , reach_amount reachAmount, face_value faceValue, discount, over_amount overAmount " +
            "FROM coupon " +
            "WHERE id=#{couponId}")
    CouponDO getOrderDetail(long couponId);

    int allCoupon();

    @Update("UPDATE coupon SET total_use = total_use + 1 WHERE id=#{couponId}")
    int useIncrease(@Param("couponId") long couponId);

    @Update("UPDATE coupon SET total_use = total_use - 1 WHERE id=#{couponId}")
    void useDecrease(Long couponId);

    @Update("UPDATE coupon SET coupon_status = #{status} WHERE total_issuance > total_get AND NOW() BETWEEN get_start_time AND get_end_time")
    void startCoupon(int status);

    @Update("UPDATE coupon SET coupon_status = #{status} WHERE id= #{couponId}")
    void updateStatus(@Param("status") int status, @Param("couponId") Long couponId);

    @Update("UPDATE coupon SET coupon_status = #{status} WHERE  NOW() < get_start_time ")
    void waitCoupon(@Param("status") int status);


    @Select("SELECT id FROM coupon WHERE shop_id=#{shopId} and is_able_activity=1 and is_delete=0 and is_enable=1 and stock>0 and coupon_status = 1")
    List<Long> searchActivityCoupon(long shopId);

    @Select("SELECT COUNT(*) " +
            "FROM coupon c " +
            "LEFT JOIN member_coupon mc ON mc.coupon_id = c.id " +
            "WHERE " +
            "NOW() BETWEEN use_start_time AND use_end_time " +
            "AND c.is_enable = 1 " +
            "AND mc.is_enable = 1 AND mc.is_use = 0 AND mc.is_expired = 0 " +
            "AND mc.is_delete = 0 AND c.model_type=#{modelType} " +
            "AND mc.member_id= #{uid}")
    int countCount(@Param("modelType") Integer modelType, @Param("uid") Long uid);



    @Select("SELECT DISTINCT " +
            "uid,spu_id spuId " +
            "FROM  " +
            "foot_order_sheet os " +
            "LEFT JOIN foot_order o ON o.order_no = os.order_no " +
            "LEFT JOIN foot_order_detail od ON od.order_id = o.id " +
            "WHERE " +
            "os.out_trade_no = #{outTradeNo}")
    List<BuySpuUserVO> couponByPay(String outTradeNo);
}
